package com.sanpu.kxe.business.login.view;

import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;

import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.login.contract.LoginContract;
import com.sanpu.kxe.business.login.presenter.LoginPresenter;
import com.sanpu.kxe.module.widget.EditTextWithDel;
import com.sanpu.kxe.utils.IntentTool;
import com.sanpu.kxe.utils.SystemBarHelper;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Description：登陆页
 * Created by 薛勇军 on 2018/6/12.
 */

public class LoginActivity extends BaseTitleActivity implements LoginContract.View {

    @Bind(R.id.user_name)
    EditTextWithDel mUserName;
    @Bind(R.id.password)
    EditTextWithDel mPassword;
    @Bind(R.id.headerView)
    LinearLayout headerView;

    private LoginContract.Presenter mPresenter;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {
        super.initView();
        int barHeight = SystemBarHelper.getStatusBarHeight(getContext());
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) headerView.getLayoutParams();
        params.setMargins(0, barHeight, 0, 0);
        headerView.setLayoutParams(params);

        mPresenter = new LoginPresenter(this);
    }

    @OnClick({R.id.registerTv, R.id.login, R.id.forget_password})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.registerTv:
                IntentTool.startActivity(mContext, RegisterActivity.class);
                break;
            case R.id.login:
                String userName = mUserName.getText().toString();
                String password = mPassword.getText().toString();
                mPresenter.login(userName, password);
                break;
            case R.id.forget_password:
                mPresenter.forgetPwd();
                break;
        }
    }

    @Override
    public void setPresenter(LoginContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return mContext;
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    protected boolean hasCommonTitle() {
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
